//
//  EssayOfNet.m
//  shusheng
//
//  Created by rimi on 16/7/23.
//  Copyright © 2016年 yuying. All rights reserved.
//

#import "EssayOfNet.h"
#import "NewCreatEssay.h"

@implementation EssayOfNet

//增
+ (void)essaySynchronizationOfNetWork:(NSDictionary *)dic{

    int essayId = [dic[@"essayID"] intValue];
//    int userId = [[AVUser currentUser].objectId intValue];
    AVObject *essay = [AVObject objectWithClassName:@"Essay"];
//    [essay setObject:@(userId) forKey:@"userID"];
    [essay setObject:[AVUser currentUser].objectId forKey:@"userID"];
    [essay setObject:@(essayId) forKey:@"essayID"];
    [essay setObject:dic[@"essayName"] forKey:@"essayName"];
    [essay setObject:dic[@"time"] forKey:@"time"];
    [essay setObject:dic[@"essayString"] forKey:@"essayString"];
   
    [essay saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        if (succeeded) {
            NSLog(@"%@",essay.objectId);
            //存objectId并且essayString更新txt
            [NewCreatEssay writeObjectId:essay.objectId txtString:dic[@"essayString"] essayId:dic[@"essayID"]];
            [MBProgressHUD showSuccess:@"同步成功"];
        } else {
            // 失败的话，请检查网络环境以及 SDK 配置是否正确
            [MBProgressHUD showSuccess:@"同步失败"];
        }
    }];
    
}

//获取用户所有的表
+ (NSArray *)readOwnEssaysOfUserId:(NSString *)userId{
    
    __block NSArray *ownEssays = [[NSArray alloc]init];
//    int uID = [userId intValue];
    AVQuery *query = [AVQuery queryWithClassName:@"Essay"];
//    [query whereKey:@"userId" equalTo:[NSNumber numberWithInt:uID]];
    [query whereKey:@"userId"containsString:userId];
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
       ownEssays = objects;
    }];
    return ownEssays;
}
//网络数据查询一个文章是否存在
+ (BOOL)readOfNetWorkOneEssayID:(NSString *)essayId{
    __block BOOL yesOrNo = NO;
    int eID = [essayId intValue];
//    int userId = [[AVUser currentUser].objectId intValue];
    
    AVQuery *essay1 = [AVQuery queryWithClassName:@"Essay"];
    [essay1 whereKey:@"userID" containsString:[AVUser currentUser].objectId];
    AVQuery *essay2 = [AVQuery queryWithClassName:@"Essay"];
    [essay2 whereKey:@"essayID" equalTo:[NSNumber numberWithInt:eID]];
    AVQuery *essay = [AVQuery andQueryWithSubqueries:[NSArray arrayWithObjects:essay1,essay2,nil]];
    
    [essay findObjectsInBackgroundWithBlock:^(NSArray *results, NSError *error) {

        if (results.count == 0) {
            yesOrNo = NO;//未同步
            
        }else{
            yesOrNo = YES;//已同步
            
        }

    }];
    return yesOrNo;
}


//    //信号
//    //串行队列
//     dispatch_queue_t mainQueue = dispatch_get_main_queue();
//     dispatch_queue_t globalQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
//     dispatch_queue_t serialQueue = dispatch_queue_create("com.rimionline.serial", DISPATCH_QUEUE_SERIAL);
//    
//     dispatch_async(globalQueue, ^{
//       
//        //数据
//        [essay findObjectsInBackgroundWithBlock:^(NSArray *results, NSError *error) {
//            NSLog(@"_____________________resultcount_%lu____________________000000000000000000000000000000000000000000000000000000000000000000000000000000000000___%@",(unsigned long)results.count,results);
//            if (results.count == 0) {
//                yesOrNo = NO;//未同步
//                
//            }else{
//                yesOrNo = YES;//已同步
//                
//            }
//            
//        }];
//        //然后
//        dispatch_sync(mainQueue, ^{
//            return yesOrNo;
//
//        });
//
//    });
    
//    NSInteger a = 0;
////    dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
//    dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
//    //数据
//    [essay findObjectsInBackgroundWithBlock:^(NSArray *results, NSError *error) {
//        NSLog(@"_____________________resultcount_%lu_____________________%@",(unsigned long)results.count,
//              results);
//        if (results.count == 0) {
//            yesOrNo = NO;//未同步
//
//        }else{
//            yesOrNo = YES;//已同步
//
//        }
//        
//        dispatch_semaphore_signal(semaphore);  //发出已完成的信号
//    }];
//    
//    //等待执行，不会占用资源
//    dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
//    NSLog(@"__________等待执行___________resultcount_%lu___________________",a);
//    return yesOrNo;
    

//更新
+ (BOOL)updataOneEssayOfObjectId:(NSString *)objectId
                     essayString:(NSString *)essayString{
    __block BOOL yesOrNo = NO;

    NSString *str = [NSString stringWithFormat:@"update Essay set essayString='%@' where objectId='%@'",essayString,objectId];
    [AVQuery doCloudQueryInBackgroundWithCQL:str  callback:^(AVCloudQueryResult *result, NSError *error) {
        if (error==nil) {
            yesOrNo = YES;
        }
        
    }];
    return yesOrNo;

}
//删除
+ (void)deleteOneEssayOfObjectId:(NSString *)objectId{
    
    __block BOOL yesOrNo = NO;
    
    NSString *str = [NSString stringWithFormat:@"delete from Essay where  objectId='%@'",objectId];
    
    [AVQuery doCloudQueryInBackgroundWithCQL:str callback:^(AVCloudQueryResult *result, NSError *error) {
        if (error == 0) {
            yesOrNo = YES;
        }
    }];
}

@end
